package com.qire.manhua.controller;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.manhua.activity.ComicReaderFinishActivity;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.ActivityComicReaderFinishBinding;
import com.qire.manhua.dialog.MessageEnablePromptDialog;
import com.qire.manhua.model.viewModel.impl.ComicReaderFinishViewModel;

import androidx.annotation.NonNull;

public class ComicReaderFinishController extends EventController<ComicReaderFinishActivity> {

    private final int dp100 = EquipmentInfoUtil.dip2px(100);

    private ComicReaderFinishViewModel viewModel;

    public ComicReaderFinishController(@NonNull ComicReaderFinishActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityComicReaderFinishBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setComicReaderFinishController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicReaderFinishViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void closeBtnOnClick(View view){
        owner.finish();
    }

    private float startX,startY;
    private float distanceX = EquipmentInfoUtil.getScreenWidth() / 2, distanceY = EquipmentInfoUtil.getScreenHeight() / 4;
    public boolean layoutOnTouch(View view, MotionEvent event) {
        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN : {
                startX = event.getX();
                startY = event.getY();
                break;
            }
            case MotionEvent.ACTION_MOVE : {
                if(startY >= event.getY())
                    break;
                final ActivityComicReaderFinishBinding viewDataBinder = owner.viewDataBinder();
                if(viewDataBinder.comicReaderView.getScroll().y!=0)
                    break;
                if(((ViewGroup.MarginLayoutParams)viewDataBinder.comicTopHead.getLayoutParams()).topMargin<dp100)
                    break;
                if(distanceY < (event.getY()-startY))
                    owner.finish();
                break;
            }
            case MotionEvent.ACTION_CANCEL :
            case MotionEvent.ACTION_UP : {
                if(distanceY < Math.abs(event.getY()-startY))
                    break;
                if(distanceX > (event.getX()-startX))
                    break;
                owner.finish();
                break;
            }
        }
        return false;
    }

    public void collectBtnOnClick(View view){
        if(LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            return;
        }
        viewModel.whetherToFavorites();

        MessageEnablePromptDialog.showIfNeed(view.getContext());
    }

    public void detailsBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                .withData("comicBookId", viewModel.comicId())
                .navigation();
        owner.finish();
    }

    public void nextBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                .withData("comicBookEntity", viewModel.readNext())
                .navigation();
        owner.onBackPressed();
    }

}
